package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.ArithmeticDeployManage;
import com.ideal.manage.esms.bean.project.ArithmeticManage;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.programme.ArithmeticDeployManageRepository;
import com.ideal.manage.esms.repository.programme.ArithmeticManageRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by 83730 on 2018/4/24.
 */
@Service
@CacheConfig(cacheNames = "arithmeticManageCache")
public class ArithmeticManageService {

    @Autowired
    private ArithmeticManageRepository arithmeticManageRepository;

    @Autowired
    private ArithmeticDeployManageRepository arithmeticDeployManageRepository;
    /**
     * 分页 根据创建时间排序
     *
     * @param pageNum
     * @return
     */
    @Cacheable(value = "arithmeticManagePage")
    public Page<ArithmeticManage> findAllManages(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<ArithmeticManage> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<ArithmeticManage> page = arithmeticManageRepository.findAll(mySpecifications, pageable);
        return page;
    }

    @CacheEvict(value = "arithmeticManagePage", allEntries = true)
    public void saveArithmetic(String name,Integer version,String remark,MultipartFile filename) throws IOException {
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        ArithmeticManage arithmeticManage=new ArithmeticManage();


        arithmeticManage.setCreateUser(new User(shiroUser.getId()));

    }

    /**
     * 分页 根据创建时间排序
     *
     * @param pageNum
     * @return
     */
    @Cacheable(value = "arithmeticDeployManagePage")
    public Page<ArithmeticDeployManage> findAllDeployManages(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<ArithmeticDeployManage> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<ArithmeticDeployManage> page = arithmeticDeployManageRepository.findAll(mySpecifications, pageable);
        return page;
    }

}
